package com.yjd.callback;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.OrderRefuseEnum;
import com.yjd.comm.fundservice.dto.QKLoanNoticeModel;
import com.yjd.comm.fundservice.util.QianKangServerUtil;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.util.FrameUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * 第三方资金服务相关操作
 * Created by yangchangyan on 2018/1/26.
 */
@Controller("com.yjd.callback.CallbackControl")
@Scope("prototype")
public class CallbackControl extends BaseControl {

    @Resource
    private IOrderService orderService;

    /**
     * 乾康 贷款通知回调
     */
    @RequestMapping(value = "/qk/callback/loan")
    public void qKCallback(String msg) throws Exception {

        Assert.hasLength(msg, "信息为空");

        logger.info("接到回调信息："+msg);

        // 贷款通知信息
        QKLoanNoticeModel loanApplyNotice = QianKangServerUtil.parseResponse(msg, QKLoanNoticeModel.class);

        OrderModel order = orderService.getModelOne(FrameUtil.newHashMap("order_no", loanApplyNotice.getAgent_order_id()), DbWREnums.READ);
        Assert.notNull(order, "订单不存在");

        // 放款结果，1成功，2失败，3审核拒绝
        switch (loanApplyNotice.getRemit_result()){
            case 1:{
                orderService.merchantConfirm(order.getOrg_id(), order.getOrder_id());
                break;
            }
            case 2:{ }
            case 3:{ }
            default:{
                orderService.merchantRefuse(order.getOrg_id(), order.getOrder_id(), OrderRefuseEnum.C, loanApplyNotice.getMsg());
            }
        }

    }
}
